#!/usr/bin/perl

package Utils;

use strict;
use warnings;

use Exporter qw(import);
our @EXPORT_OK = qw(trim dd);

use Data::Dumper qw(Dumper);
use Encode qw(encode);
use File::Spec;

sub trim
{
    my $s = shift;
    $s =~ s/^\s+|\s+$//g;
    return $s;
}

sub dd
{
    my ($package, $filename, $line) = caller;

    print ">>> File: ${filename}, line: $line, type: ". ref($_[0]). "\n";
    my $output = Dumper(@_);
    print encode('UTF-8', $output);
}

sub readFile
{
    my $filename = shift;
    open(my $fh, '<', $filename) or die "Could not open file '$filename' $!";
    my @lines = <$fh>;
    close $fh;
    my $content = join('', @lines);
    return $content;
}

sub isDate {
    my ($value) = @_;
    return $value =~ /^\d{2}-\w{3}-\d{2}$|^\d{4}-\d{2}-\d{2}$/;
}

sub _mkdir {
    my ($path, %options) = @_;
    
    $options{mode} //= 0777;
    
    # 规范化路径
    $path = File::Spec->rel2abs($path);
    my @parts = File::Spec->splitdir($path);
    
    my $current_path = '';
    foreach my $part (@parts) {
        next if $part eq '';
        $current_path = File::Spec->catdir($current_path, $part);
        
        unless (-d $current_path) {
            mkdir $current_path, $options{mode}
                or do {
                    warn "无法创建目录 $current_path: $!";
                    return 0;
                };
            print "创建目录: $current_path\n" if $options{verbose};
        }
    }
    
    return 1;
}

# sub dd
# {
#     my ($package, $filename, $line) = caller;
#     print ">>> File: ${filename} line $line\n";

#     for my $i (0..$#_) {
#         my $ref = ref($_[$i]);
#         print "[$i] $ref\n";
#         if ($ref eq 'SCALAR') {
#             print "    $_[$i]\n";
#         } elsif ($ref eq 'ARRAY') {
#             for my $j (0..$#{$_[$i]}) {
#                 print "    $j: $_[$i]->[$j]\n";
#             }
#         } elsif ($ref eq 'HASH') {
#             for my $key (keys %{$_[$i]}) {
#                 print "    $key: $_[$i]->{$key}\n";
#             }
#         } else {
#             print "    $_[$i]\n";
#         }
#     }

#     exit 1;
# }

1;

__END__
